function [] = profplot(prof_mean, prof_sem, prof_norm, prof_ref);

% PROFPLOT - create profile figures
%
%    PROFPLOT(MEAN, SEM) creates displays of profile averages super-
%    imposed on regions representing profile mean +/- SEM.
%
%    PROFPLOT(MEAN, SEM, NORM) superimposes normalized profiles on the
%    regions representing mean +/- SEM.
%
%    PROFPLOT(MEAN, SEM, NORM, REF) adds reference lines.

% By:   S.C. Molitor (smolitor@med.unc.edu)
% Date: May 10, 2000

% default parameters

edge_color = 'none';
face_color = 3*[1 1 1]/4;

line_color = [0 0 0];
line_width = 1;
line_style = '-';

ref_color = [0 0 0];
ref_width = 0.5;
ref_style = '-';

% validate arguments

if ((nargin < 2) | (nargin > 4))
   msgbox('Invalid number of arguments', 'PROFPLOT Error', 'warn');
   return
elseif (~isnumeric(prof_mean) | isempty(prof_mean))
   msgbox('MEAN must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (~isnumeric(prof_sem) | isempty(prof_sem))
   msgbox('SEM must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (any(size(prof_sem) ~= size(prof_mean)))
   msgbox('SEM must be the same size as MEAN', 'PROFPLOT Error', 'warn');
   return
elseif (nargin == 2)
   prof_norm = [];
   prof_ref = [];
elseif (~isnumeric(prof_norm))
   msgbox('NORM must be a numeric array', 'PROFPLOT Error', 'warn');
   return
elseif (nargin == 3)
   prof_ref = [];
elseif (~isnumeric(prof_ref))
   msgbox('REF must be a numeric array', 'PROFPLOT Error', 'warn');
   return
end

% create figure & axes

h_fig = figure;
h_axes = axes;

% create mean +/- SEM regions

for i = 2 : size(prof_mean, 2)
   h_patch(i - 1) = patch([prof_mean(:, 1); flipud(prof_mean(:, 1))], ...
      [prof_mean(:, i) - prof_sem(:, i); flipud(prof_mean(:, i) + prof_sem(:, i))], ...
      face_color, 'EdgeColor', edge_color);
end

% plot norm lines if provided
% otherwise plot mean lines

if (isempty(prof_norm))
   for i = 2 : size(prof_mean, 2)
      h_line(i - 1) = line(prof_mean(:, 1), prof_mean(:, i), ...
         'Color', line_color, 'LineWidth', line_width, 'LineStyle', line_style);
   end
else   
   for i = 2 : size(prof_norm, 2)
      h_line(i - 1) = line(prof_norm(:, 1), prof_norm(:, i), ...
         'Color', line_color, 'LineWidth', line_width, 'LineStyle', line_style);
   end
end

% plot reference lines if given

for i = 2 : 2 : size(prof_ref, 1)
   h_ref(i/2) = line(prof_ref(i - 1, :), prof_ref(i, :), ...
      'Color', ref_color, 'LineWidth', ref_width, 'LineStyle', ref_style);
end
return
